package com.springBoot.springBootSysCore.modules.repository.system;

import com.springBoot.springBootSysCore.common.repository.IBaseRepository;
import com.springBoot.springBootSysCore.modules.entity.system.SysOrganization;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * 机构Repository
 * @author PSC
 * 2015年11月30日
 */
public interface SysOrganizationRepository extends IBaseRepository<SysOrganization, String> {
	/**
	 * 查询所有父机构
	 * @author PSC
	 * @return
	 */
	@Query("from SysOrganization t where t.parentOrganizations is null order by createDate desc")
	public List<SysOrganization> findAllParent();
	/**
	 * 根据用户id查询机构id集合
	 * @param userId
	 * @return
	 */
	@Query("select org.id from SysOrganization org left join org.sysUsers as user  where user.id = ?1")
	public List<String> findIdByUser(String userId);
	/**
	 * 根据父Id集合查询他们的子节点集合(删除验证)
	 * @author PSC
	 * @param fatherList
	 * @return
	 */
	@Query("from SysOrganization where parentOrganizations.id in (?1)")
	public List<SysOrganization> findByParent(List<String> fatherList);
	/**
	 * 根据Id批量删除
	 * @author PSC
	 * @param asList
	 */
	@Modifying
	@Query("delete from SysOrganization where id in(?1)")
	public void deleteOrganizationsByIds(List<String> asList);
	/**
	 * 通过用户Id查询他拥有的机构(查询用户zTree树用到)
	 * @author PSC
	 * @param sysUserId
	 * @return
	 */
	@Query("from SysOrganization t inner join t.sysUsers user with user.id=?1 where t.parentOrganizations is null")
	public List<SysOrganization> findFatherOrgIdsByUser(String sysUserId);
	
	/**
	 * 根据机构id集合查询机构集合
	 * @param orgIds
	 * @return
	 */
	@Query("from SysOrganization where id in(?1)")
	public List<SysOrganization> findOrgList(List<String> orgIds);
	/**
	 * 根据机构id集合查询机构name集合
	 * @param orgIds
	 * @return
	 */
	@Query("select o.name from SysOrganization o where o.id in(?1)")
	public List<String> getOrgNameList(List<String> orgIds);
	
	@Query("select o.id from SysOrganization o where o.name = ?1")
	public List<String> queryIdByName(String name);

}
